package org.ko.core.logic.impl;

import org.ko.core.dao.LogInfoDao;
import org.ko.core.dao.support.Finder;
import org.ko.core.domain.support.LogInfo;
import org.ko.core.logic.LogInfoService;
import org.ko.core.websupport.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
public class LogInfoServiceImpl implements LogInfoService{
	
	@Autowired
	private LogInfoDao logInfoDao;
	
	@Override
	@Transactional
	public void save(LogInfo log) throws Exception {
		logInfoDao.save(log);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Page findLog(Page page, String category, String keyword,
			long userId) throws Exception {
		Finder finder = Finder.create("FROM LogInfo where 1=1" );
		
		if(userId > 0)
			finder.append(" and userId="+userId);
		
		if(StringUtils.hasText(category)){
			finder.append(" and category=:cat");
			finder.setParam("cat", category);
		}
		
		if(StringUtils.hasText(keyword)){
			finder.append(" and (message like :k or remark like :k)");
			finder.setParam("k", "%"+keyword+"%");
		}
		
		finder.append(" order by id desc");
		System.out.println(finder.getOrigHql());
		return logInfoDao.find(finder, page.getPageNo(), page.getPageSize());
	}
	
}